(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取下拉框类型
	function getlinklist(selectlist,selectid){
	   $.ajax({
			url:'http://web.icecn.net/web/findInfoChannelList.json',
			type:'POST',
			async:true,
			data:{
                offset:0,
				limit:100
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					
					if(dataset && dataset.length > 0){ 
					   //var dataset2=[];
//					   for(var i=0;i<dataset.length;i++){
//						   if(dataset[i].isShow==0){
//							  dataset2.push(dataset[i])
//						   }
//					   }
					   var html = template(selectlist,{dataset:dataset});
					   $("#"+selectid).html(html);
					};
				}
			}
	   });	
	}
	function getlinklist2(selectlist,selectid,type){
	   $.ajax({
			url:'http://web.icecn.net/web/getHomeEnum.json',
			type:'POST',
			async:true,
			data:{
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.data;
					var d=new Array();
					if(dataset && dataset.length > 0){
					   for(var i=0;i<dataset.length;i++){
						   if(dataset[i].enumType==type){
							   d.push(dataset[i])
						   }  
					   }
					   dataset=d;
					   var html = template(selectlist,{dataset:dataset});
					   $("#"+selectid).html(html);
					};
				}
			}
	   });	
	}
	//获取列表
	function getlist(curpage,pagenum){
	   var informationName=$("#informationName1").val();
	   var belongsChannel=$("#channelType1 option:selected").attr("data-id")>0?parseInt($("#channelType1 option:selected").attr("data-id")):"";
	   var informationType=$("#informationType1 option:selected").attr("data-id")>0?parseInt($("#informationType1 option:selected").attr("data-id")):"";
	   var author=$("#author1").val();
	   var isShow=$("#isShow1").val();
	   if(isShow=="全部"){isShow=""}else if(isShow=="是"){isShow=0}else{isShow=1}
	   var startTime=$("#datepicker").val()==""?"":$("#datepicker").val()+" 00:00:00";
	   var endTime=$("#datepicker2").val()==""?"":$("#datepicker2").val()+" 23:59:59";
	   $.ajax({
			url:'http://web.icecn.net/web/findInfoContentList.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				informationName:informationName,
				belongsChannel:belongsChannel,
				informationType:informationType,
				author:author,
				startTime:startTime,
				endTime:endTime,
				isShow:isShow
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else(
					  $("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>")
					);
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".searchbtn").click(function(){
				curpage=1
				getlist(curpage,pagenum)
			})
			//全选
			$(".content").on("click", '.allcheck', function() {
				if ($(".allcheck").attr("checked")) { 
				    $(".allcheck").attr("checked", false)
					$("input[name=items]").each(function() {  
						$(this).attr("checked", false);  
					});  
				} else {  
				    $(".allcheck").attr("checked", true)
					$("input[name=items]").each(function() {  
						$(this).attr("checked", true);  
					});  
				}  
			})
			//删除按钮
			$(".content-wrapper").on("click", '.delete', function() {
				if($(this).attr("data-id")>0){
				   $("#modal-delete .modal-body").attr("data-id",$(this).attr("data-id"))
				   $("#modal-delete .modal-body").html("确认删除该数据吗？")
				}else{
				   $("#modal-delete .modal-body").removeAttr("data-id");
				   $("#modal-delete .modal-body").html("确认批量删除数据吗？")
				}
			})
			//确认删除
			$(".btndelete").click(function(){
			   var id=$("#modal-delete .modal-body").attr("data-id");
			   if(id>0){
				    id=$("#modal-delete .modal-body").attr("data-id");
			   }else{
				    id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
					id=id.substr(1)
			   }
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/web/deleteInfoContent.json',
					type:'POST',
					async:true,
					data:{
						id:id
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-delete").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("删除失败")	
						}
					}
			   });
		    })
			//批量显示隐藏按钮
			$(".content-wrapper").on("click", '.isshow', function() {
				var isshow=$(this).attr("data-isshow");
				$("#modal-isshow .modal-body").html("确认"+$(this).html()+"数据?")
				$("#modal-isshow .modal-body").attr("data-is",isshow)
				
			})
			//确认显示隐藏
			$(".btnisshow").click(function(){
			   id="";
			   $("input[name=items]").each(function() {
					if ($(this).prop("checked")) {
						id += ","+$(this).val();
					}
			   }); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   var keyWord=$("#modal-isshow .modal-body").attr("data-is")
			   $.ajax({
					url:'http://web.icecn.net/web/updateInfoContentIsShow.json',
					type:'POST',
					async:true,
					data:{
						id:id,
						keyWord:keyWord
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-isshow").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("操作失败")	
						}
					}
			   });
		    })
			//批量置顶按钮
			$(".content-wrapper").on("click", '.isstick', function() {
				var isshow=$(this).attr("data-isstick");
				$("#modal-isstick .modal-body").attr("data-is",isshow)
				$("#modal-isstick .modal-body").html("确认"+$(this).html()+"数据?")
			})
			//确认置顶
			$(".btnisstick").click(function(){
			   id="";
			   $("input[name=items]").each(function() {
					if ($(this).prop("checked")) {
						id += ","+$(this).val();
					}
			   }); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   var keyWord=$("#modal-isstick .modal-body").attr("data-is")
			   $.ajax({
					url:'http://web.icecn.net/web/updateInfoContentIsStick.json',
					type:'POST',
					async:true,
					data:{
						id:id,
						keyWord:keyWord
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-isstick").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("操作失败")	
						}
					}
			   });
		    })
			//批量修改权重
			$(".btnWeight").click(function(){
				id="";
			   $("input[name=items]").each(function() {
					if ($(this).prop("checked")) {
						id += ","+$(this).val();
					}
			   }); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   var keyWord=$(".updaterank").val();
				$.ajax({
					url:'http://web.icecn.net/web/updateInfoContentRank.json',
					type:'POST',
					async:true,
					data:{
						id:id,
						keyWord:keyWord
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-Weight").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("操作失败")	
						}
					}
			   });  
			})
			//加载栏目类型
			$("#channelType2").change(function() {
			   $("#informationType2").val($("#channelType2 option:selected").attr("data-channeltype"))
			})
			//预览按钮
			$(".content-wrapper").on("click", '.view', function() {
				var id=$(this).attr("data-id");
				$.ajax({
						url:'http://web.icecn.net/web/findInfoContent.json',
						type:'POST',
						async:true,
						data:{
							id:id
						},
						dataType: "json",
						xhrFields:{withCredentials:true},
						success: function(data){
							if(data.code === 200 ){
							  var d= data.t;
							  $(".informationName").html(d.informationName);
							  $(".channelType2").html(d.belongsChannel);
							  $(".author").html(d.author);
							  $(".isShow").html(d.isShow==0?"是":"否");
							  $(".photoCover1").html(d.pictureUrl);
							  $(".imgfile1").html("<img src='"+d.pictureUrl+"'/>");
							  $(".relevance").html(d.relevance);
							  $(".relevanceId").html(d.relevanceId);
				              CKEDITOR.instances.editor2.setData(d.content)
							  $(".rank").html(d.rank);
							  $(".informationType2").html($("#channelType2 option:selected").attr("data-channeltype"))
							}
						}
				  });
			})
			//新建修改按钮
			$(".content-wrapper").on("click", '.addupdate', function() {
				var id=$(this).attr("data-id");
				if(id!=null){
				  $("#modal-addupdate .modal-title").html("修改资讯内容");
				  $("#id").val(id);
				  $.ajax({
						url:'http://web.icecn.net/web/findInfoContent.json',
						type:'POST',
						async:true,
						data:{
							id:id
						},
						xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
							  var d= data.t;
							  $("#informationName").val(d.informationName);
							  $("#channelType2").val(d.belongsChannel);
							  $("#author").val(d.author);
							  $("#isShow").val(d.isShow==0?"是":"否");
							  $("#photoCover1").val(d.pictureUrl);
							  $(".imgfile1").html("<img src='"+d.pictureUrl+"'/>");
							  $("#relevance").val(d.relevance);
							  $("#relevanceId").val(d.relevanceId);
				              CKEDITOR.instances.editor1.setData(d.content)
							  $("#rank").val(d.rank);
							  $("#informationType2").val($("#channelType2 option:selected").attr("data-channeltype"))
				              $("#lefile1").val("");
							}
						}
				  });
				}else{
				  $("#modal-addupdate .modal-title").html("新建资讯内容")
				  $("#id").val("");
				  $("#informationName").val("");
				  $("#rank").val("");
				  $("#channelType2 option:first").prop("selected", 'selected');
				  $("#author").val("");
				  $("#isShow option:first").prop("selected", 'selected');
				  $("#photoCover1").val("");
				  $(".imgfile1").html("");
				  $("#lefile1").val("");
				  CKEDITOR.instances.editor1.setData("");
				  $("#relevance option:first").prop("selected", 'selected');
				  $("#relevanceId").val("");
				  $("#informationType2").val($("#channelType2 option:first").attr("data-channeltype"));
				}
			})
			//确认修改按钮
			$(".btnaddupdate").click(function(){
				 var id=$("#id").val();
				 var contentId=$("#channelType2 option:selected").attr("data-channelid");
				 var pictureUrl=$("#photoCover1").val();
				 var informationName=$("#informationName").val();
				 var informationType=parseInt($("#informationType2 option:selected").attr("data-id"));
				 var belongsChannel=parseInt($("#channelType2 option:selected").attr("data-id"));
				 var author=$("#author").val();
				 var content=CKEDITOR.instances.editor1.getData();
				 var rank=$("#rank").val();
				 var relevance=$("#relevance").val();
				 var relevanceId=$("#relevanceId").val();
				 var isShow=$("#isShow").val()=="是"?0:1;
				 if(informationName==""){alert("请输入标题"); return}
				 if(informationName.length>24){alert("标题不能大于24个字"); return}
				 if(author.length>10){alert("作者不能大于10个字"); return}
				 var datas={};
				 if(id>0){
					 if(relevanceId==""){
						 datas={
							id:id,
							contentId:contentId,
							pictureUrl:pictureUrl,
							informationName:informationName,
							informationType:informationType,
							belongsChannel:belongsChannel,
							author:author,
							content:content,
							rank:rank,
							relevance:relevance,
							isShow:isShow
						}
					 }else{
						 datas={
							id:id,
							contentId:contentId,
							pictureUrl:pictureUrl,
							informationName:informationName,
							informationType:informationType,
							belongsChannel:belongsChannel,
							author:author,
							content:content,
							rank:rank,
							relevance:relevance,
							relevanceId:relevanceId,
							isShow:isShow
						} 
					 }
					 $.ajax({
						url:'http://web.icecn.net/web/updateInfoContent.json',
						type:'POST',
						async:true,
						data:datas,
				    	xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert("操作失败")	
							}
						}
				     });
				 }else{
					 
					 if(relevanceId==""){
						 datas={
							contentId:contentId,
							pictureUrl:pictureUrl,
							informationName:informationName,
							informationType:informationType,
							belongsChannel:belongsChannel,
							author:author,
							content:content,
							rank:rank,
							relevance:relevance,
							isShow:isShow,
							isstick:1
						}
					 }else{
						 datas={
							contentId:contentId,
							pictureUrl:pictureUrl,
							informationName:informationName,
							informationType:informationType,
							belongsChannel:belongsChannel,
							author:author,
							content:content,
							rank:rank,
							relevance:relevance,
							relevanceId:relevanceId,
							isShow:isShow,
							isstick:1
						} 
					 }
					 $.ajax({
						url:'http://web.icecn.net/web/insertInfoContent.json',
						type:'POST',
						async:true,
						data:datas,
					    xhrFields:{withCredentials:true},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								$("#modal-addupdate").modal('hide');
								getlist(curpage,pagenum);
							}else{
								alert("操作失败")	
							}
						}
				     });
				 }
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
			getlinklist("selectlist1","channelType1");
			getlinklist("selectlist2","channelType2");
			getlinklist2("selectlist3","informationType1",2);
			getlinklist2("selectlist4","informationType2",2);
		}
	};
	Page.init();
})();